using System;
using System.Collections.Generic;

namespace Assets.Common.System
{
    public static class CollectionsExtension
    {
        //不好用
        public static void SafeRemove<T>(this List<T> list, ref int idx)
        {
            list[idx] = list[^1];
            list.RemoveAt(list.Count-1);
            idx--;
        }
        /// <summary>
        /// 校验 idx 是否是array 的合法下标
        /// </summary>
        /// <param name="array"></param>
        /// <param name="idx"></param>
        /// <returns></returns>
        public static bool IsValidIndex(this Array array, int idx)
        {
            if(idx <0 || idx > array.Length-1)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        /// <summary>
        /// 校验 idx arrayLenth长度数组 的合法下标
        /// </summary>
        /// <param name="arrayLenth"></param>
        /// <param name="idx"></param>
        /// <returns></returns>
        public static bool IsValidIndex(this int arrayLenth, int idx)
        {
            if (idx < 0 || idx > arrayLenth - 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    
        public static T GetRandom<T>(this List<T> list)
        {
            return list[UnityEngine.Random.Range(0, list.Count-1)];
        }
    }
}
